package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * RenameCategory Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by Tan Si Ling (A0085314J)
 */

//modify category needs to be supported by API : check for duplicate naming.

import cli.tools.Process;
import shared.*;

class RenameCategory extends Function{

	private static final String[] SELECTCAT = {
			"-= Main menu > Manage Categories > Rename a Category =-",
			"[b]: back [u]: undo [r]: redo [h]: home menu", "",
			"Select a Category by its index" };

	private static final String[] ENTERNEWNAME = { "Enter the new name for this category: " };

	private static final State BACK = State.MANAGECATEGORIES;
	private static final State CURRENT = State.RENAMECATEGORY;

	public static State runMenu() {

		cli.print(SELECTCAT);
		Object choice = tools.selectFromList(cli.getApi().getCategoryList(),
				"manageCategories");

		// if back signal sent
		if (choice == null) {
			return BACK;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// last case : if choice was really the correct object
		Category toChange = (Category) choice;

		if (toChange.getName().equals("cashLoanRepayment")) {
			cli.println("We're sorry, the category 'cashLoanRepayment' cannot be modified");
			return State.RENAMECATEGORY;
		}

		if (toChange.equals(cli.getApi().getCategoryList().get(0))) {
			cli.println("Unable to rename 'Uncategorised'");
			cli.println("Returning to previous menu");
			return CURRENT;
		}

		cli.print(ENTERNEWNAME);
		
		String input = cli.nextLine();
		
		//TODO - potential problem here
		// Si Ling: **Keeping Track of the Actions DONE**
		tools.action.addToUndoStack(toChange.getName(),
				input, Process.MODIFYCATEGORYNAME, toChange);
		// **END**

	//	toChange.modifyName(input);
		try {
			cli.getApi().modifyCategory(toChange, input);
		} catch (Exception e) {
			cli.print(ERROR);
		}

		// return to parent menu
		return CURRENT;
	}

}